<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\jia;

use app\common\model\jia\Area as AreaModel;
use app\store\model\jia\Floor as FloorModel;
use think\facade\Db;
/**
 * 文章分类模型
 * Class Category
 * @package app\store\model\article
 */
class Area extends AreaModel {

	/**
	 * 添加新记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data): bool {

		//查询是否添加重复

		$log=Db::name('jia_manage_build')->where('manage_id',$data['manage_id'])
		->where('build_id',$data['build_id'])->find();
		if($log){
			$this->error = '该楼栋已存在，请务重复添加';
		     return false;
		}
		// 保存记录
		$data['store_id'] = self::$storeId;

		//$data['floor']=json_encode($data['floor']);
		$this->save($data);
		FloorModel::increased($this['id'], $data);

		return true;
	}

	public static function increased(int $clerkId, array $shopIds) {
		$dataset = [];
		foreach ($shopIds as $imageId) {
			$dataset[] = [
				'shop_id' => $imageId,
				'manage_id' => $clerkId,
				'store_id' => self::$storeId,
			];
		}
		return (new static )->addAll($dataset);
	}

	public function getList(array $param = []): \think\Paginator {
		// 查询参数
		$params = $this->setQueryDefaultValue($param, [
			'title' => '', // 文章标题
			'manageId' => 0, // 文章分类id
			'status' => -1, // 文章状态
		]);
		// 检索查询条件
		$filter = [];
		// 文章标题
		!empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];

		$params['manageId'] > 0 && $filter[] = ['manage_id', '=', $params['manageId']];
		// 文章状态
		$params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
		// 查询列表数据
		$data = $this->with(['build', 'shop', 'floors'])
			->where($filter)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->paginate(15);
		// 获取文章的图片和分类
		return $data;
	}

	/**
	 * 编辑记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data): bool {

			//查询是否添加重复

		$log=Db::name('jia_manage_build')->where('manage_id',$this['manage_id'])
		->where('build_id',$data['build_id'])->find();
		if($log&&$log['id']!=$this['id']){
			$this->error = '该楼栋已存在，请务重复添加';
		     return false;
		}
		$data['manage_id']=$this['manage_id'];

		FloorModel::increased($this['id'], $data);
		// 保存记录
		return $this->save($data);
	}

	/**
	 * 删除商品分类
	 * @param int $categoryId
	 * @return bool
	 */
	public function remove($id) {
			Db::name('jia_manage_floor')->where('log_id', $id)->delete();

		// 删除记录
		return $this->where('id', $id)->delete();
	}
}
